package com.fuli.app.controller;

import com.fuli.app.common.ActionResult;
import com.fuli.app.common.ErrorInfo;
import com.fuli.app.common.Pagination;
import com.fuli.app.dto.SignInDTO;
import com.fuli.app.dto.query.SignInQueryDTO;
import com.fuli.app.service.SignInService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api(tags ="签到")
@RestController
@RequestMapping("/signIn")
public class SignInController {
    @Autowired
    private SignInService signInService;

    @ApiOperation(value = "签到接口",
            notes = "<b>必填属性</b><br/>" +
                    "1,userId 用户id <br/>" +
                    "2,grade 签到所得分数<br/>")
    @PostMapping("/add")
    public ActionResult<String> add(@ApiParam("签到DTO") @RequestBody SignInDTO dto) {
        List<ErrorInfo> errors = new ArrayList<>();
        if (StringUtils.isEmpty(dto.getUserId()))
            errors.add(new ErrorInfo(0, "用户id为空"));
        if (StringUtils.isEmpty(dto.getGrade()))
            errors.add(new ErrorInfo(0, "用户分数为空"));
        if (errors.size() > 0)
            return new ActionResult<>(errors);
        return signInService.add(dto);
    }

    @ApiOperation("删除签到记录")
    @DeleteMapping("/delete/{id}")
    public ActionResult<String> delete(@ApiParam("id") @PathVariable String id) {
        if (StringUtils.isEmpty(id))
            return new ActionResult<>(0, "id为空");
        return signInService.delete(id);
    }

    @ApiOperation("批量删除签到记录")
    @DeleteMapping("/beachDelete")
    public ActionResult<List<String>> beachDelete(@ApiParam("id数组") @RequestParam List<String> ids) {
        if (StringUtils.isEmpty(ids))
            return new ActionResult<>(0, "ids为空");
        return signInService.beachDelete(ids);
    }

    @ApiOperation("获取签到记录")
    @GetMapping("/get/{id}")
    public ActionResult<SignInDTO> get(@ApiParam("id") @PathVariable String id) {
        if (StringUtils.isEmpty(id))
            return new ActionResult<>(0, "id为空");
        return signInService.get(id);
    }

    @ApiOperation("分页查询")
    @GetMapping("/pageQuery")
    public ActionResult<Pagination<SignInDTO>> pageQuery(@ApiParam("SignInDTO") @ModelAttribute SignInQueryDTO dto) {
        if (dto.getPageNo() == null || dto.getPageNo() <= 0)
            dto.setPageNo(1);
        if (dto.getPageSize() == null || dto.getPageSize() <= 0)
            dto.setPageSize(10);
        return signInService.pageQuery(dto);
    }
}
